<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller {

	public $data;

	public function __construct()
	{
		parent::__construct();
		//载入模型
		$this->load->model('Login_model');
		$this->load->model('User_model');
		$this->load->model('Right_model');
		//载入库文件
		$this->load->library('Menu_control');
		$this->load->library('Forbid_resubmit');
		//$this->load->helper('cookie');
		//初始化试图变量
		$this->data = array();
		//判断是否为登录用户
		$this->data['title'] = '账户管理';
		$this->data['styles'] = array('user.css', 'error.css');
		$this->data['scripts'] = array('master.js', 'user.js');
		//检查是否是登录用户
		if (!$this->Login_model->check(TRUE))
		{
			header("Location: ".$this->data['site']['root'].'/login');
		}
		//设置站点基本参数
		$this->data['site'] = $this->config->item('site');
		$this->data['header']['username'] = $this->input->cookie('realname');
		$this->data['site']['environment'] = $this->User_model->get_envrionment();
		//获取当前用户的信息
		$this->data['cur_user'] = $this->User_model->getUser($this->input->cookie('user_id'));
		$this->data['menus'] = $this->config->item('menus');
		$this->data['menus'] = $this->menu_control->main($this->data['menus'], 8); //重新组织主菜单顺序，菜单索引在../config/site.php查看
		$this->data['sub_menus'] = $this->_menus();
		$this->data['sub_menu_index'] = '-1';
	}

	private function _menus()
	{
		return array(
			array(
				'index'	=>	1,
				'url'	=>	$this->data['site']['root'] . 'user/all/',
				'name'	=>	'所有账户',
				'right' => 'user.all'
			),
			array(
				'index'	=>	2,
				'url'	=>	$this->data['site']['root'] . 'user/create/',
				'name'	=>	'创建账户',
				'right' => 'user.create'
			),
			array(
				'index'	=>	3,
				'url'	=>	$this->data['site']['root'] . 'user/can/',
				'name'	=>	'回收账户',
				'right' => 'user.can'
			),
		);
	}

	public function index()
	{
		$this->view();
	}

	public function create()
	{
		$this->data['sub_menu_index'] = 2;
		$this->data['title'] = '新增用户';
		//判断是否是超级管理员用户
		if ($this->User_model->isSuperuser()) {
			//判断是否重复提交
			if ($this->forbid_resubmit->judge_submit($this->input->post('rand'))) {
				//验证提交的权限是否合法
				if ($this->Right_model->analyseRights())
				{
					if ($this->User_model->createUser()) {
						header('Location: ' . $this->data['site']['root'] . 'user/all/');
						return true;
					}
				}
				else
				{
					$this->data['error'] = '非法提交';
					return $this->load->view_('error_500_view', $this->data);
				}
			}
			//如果是重复提交或表单有错误
			//载入创建用户视图
			$this->data['form']['rights'] = $this->Right_model->getRights();
			$this->data['form']['rand'] = $this->forbid_resubmit->set_submit_rand();
			return $this->load->view_('user_create_view', $this->data);
		}
		$this->load->view_('error_500_view', $this->data);
	}

	//重构方法更新信息
	public function update($type, $uid = false) {
		if (false === $uid)
		{
			$uid = $this->input->cookie('user_id');
		}
		//判断被修改用户是否存在
		$this->data['form']['user'] = $this->User_model->getUser($uid);
		if ($this->data['form']['user'])
		{
			switch ($type) {
				case 'base':
					$this->data['title'] = '修改基本信息';
					$this->data['update_type'] = "基本信息";
					$this->data['update'] = 'base';
					$this->_updateBase($uid);
					break;
				case 'secure':
					$this->data['title'] = '修改安全信息';
					$this->data['update_type'] = "安全信息";
					$this->data['update'] = 'secure';
					$this->_updateSecure($uid);
					break;
				case 'rights':
					$this->data['title'] = '修改权限';
					$this->data['update_type'] = "权限";
					$this->data['update'] = 'rights';
					$this->_updateRights($uid);
					break;
				default:
					$this->data['error'] = '无法识别的操作';
					$this->load->view_('error_500_view', $this->data);
					break;
			}
			return true;
		}
		$this->load->view_('error_500_view', $this->data);
	}

	//查看用户信息
	public function view($uid = false)
	{
		$this->data['title'] = '查看用户信息';
		$this->data['user'] = $this->User_model->getUser($uid);
		$this->load->view_('user_view', $this->data);
	}

	//查看所有用户信息
	public function all()
	{
		$this->data['sub_menu_index'] = 1;
		$this->data['users'] = $this->User_model->getUsers();
		return $this->load->view_('users_view', $this->data);
	}

	public function can()
	{
		$this->data['sub_menu_index'] = 3;
		$this->data['users'] = $this->User_model->getUsers('del');
		return $this->load->view_('users_view', $this->data);
	}

	//禁用用户 AJAX
	public function del()
	{
		if ($this->User_model->isSuperuser())
		{
			$uids = $this->input->post('user_id');
			if ($this->User_model->getUser($uids, true))
			{
				foreach ($uids as $v)
				{
					$this->User_model->delUser($v);
				}
				$this->data['ajax_request'] = 'DEL_SUCCESS';
				return $this->load->view('ajax_request', $this->data);
			}
			else
			{
				$this->data['error'] = '所选取的账户存在未知账户';
			}
		}
		else
		{
			$this->data['error'] = '拒绝访问';
		}
		return $this->load->view('error_500_view', $this->data);
	}

	//恢复用户 AJAX
	public function recover()
	{
		if ($this->User_model->isSuperuser())
		{
			$uids = $this->input->post('user_id');
			if ($this->User_model->getUser($uids, true))
			{
				foreach ($uids as $v) {
					$this->User_model->recoverUser($v);
				}
				$this->data['ajax_request'] = 'RECOVER_SUCCESS';
				return $this->load->view('ajax_request', $this->data);
			}
			else
			{
				$this->data['error'] = '存在未知用户，请返回刷新！';
			}
		}
		else
		{
			$this->data['error'] = '你没有权限执行此操作，请返回！';
		}
		return $this->load->view('error_500_view', $this->data);
	}

	//更新基本信息
	private function _updateBase($uid)
	{
		//判断是否是超级用户或修改自己，且被修改用户存在
		if ($this->User_model->isSuperuser() || $uid == $this->input->cookie('user_id')) {
			//判断是否重复提交
			if ($this->forbid_resubmit->judge_submit($this->input->post('rand'))) {
				if ($this->User_model->updateUser($uid,'base'))
				{
					$this->data['form']['user'] = $this->User_model->getUser($uid);
					$this->data['form']['result'] = '编辑完成';
					$this->data['form']['rand'] = $this->forbid_resubmit->set_submit_rand();
					$this->load->view_('user_update_view', $this->data);
					return true;
				}
				else
				{
					//提交失败，重置更新信息
					$this->data['form']['result'] = '没有改动';
					unset($this->data['form']['user']['phone']);
				}
			}
			$this->data['form']['rand'] = $this->forbid_resubmit->set_submit_rand();
			//return $this->load->view_('user_update_base_view', $this->data);
			return $this->load->view_('user_update_view', $this->data);
		}
		else
		{
			$this->data['error'] = '没有权限操作，请更换帐号';
			$this->load->view_('error_500_view', $this->data);
		}
	}

	//更新安全信息
	private function _updateSecure($uid) {
		//判断是否是超级用户或本账户，且被修改用户存在
		if ($this->User_model->isSuperuser() || $uid == $this->input->cookie('user_id')) {
			//判断是否能提交
			if ($this->forbid_resubmit->judge_submit($this->input->post('rand')) && $this->User_model->updateUser($uid,'secure'))
			{
				$this->data['form']['result'] = '编辑完成';
			}
			$this->data['form']['rand'] = $this->forbid_resubmit->set_submit_rand();
			//return $this->load->view_('user_update_secure_view', $this->data);
			return $this->load->view_('user_update_view', $this->data);
		}
		return $this->load->view_('error_500_view', $this->data);
	}

	//更新权限信息
	private function _updateRights($uid) {
		//判断是否是超级用户，且被修改用户存在
		//超级用户权限不可更改
		if ($this->User_model->isSuperuser() && $uid != 1) {
			//判断能否提交
			if ($this->forbid_resubmit->judge_submit($this->input->post('rand')))
			{
				if ($this->Right_model->analyseRights())
				{
					if ($this->User_model->updateUser($uid,'rights'))
					{
						$this->data['form']['user'] = $this->User_model->getUser($uid);
						$this->data['form']['result'] = '编辑完成';
						$this->data['form']['rights'] = $this->Right_model->getRights();
						$this->data['form']['rand'] = $this->forbid_resubmit->set_submit_rand();
						$this->load->view_('user_update_view', $this->data);
						return true;
					}
					else
					{
						$this->data['form']['result'] = '没有改动';
					}
				}
				else
				{
					$this->data['error'] = '非法操作';
					$this->load->view_('error_500_view', $this->data);
				}
			}
			$this->data['form']['rights'] = $this->Right_model->getRights();
			$this->data['form']['rand'] = $this->forbid_resubmit->set_submit_rand();
			//return $this->load->view_('user_update_rights_view', $this->data);
			return $this->load->view_('user_update_view', $this->data);
		}
		$this->data['error'] = '不可执行此操作';
		$this->load->view_('error_500_view', $this->data);
	}

}

/* End of file user.php */